package io.wongxd.demo.page.emo.ui.page

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.navigation.NavBackStackEntry
import io.modifier.basic.emo.scheme.ComposeScheme
import io.modifier.basic.emo.scheme.SchemeBoolArg
import io.modifier.basic.emo.scheme.SchemeFloatArg
import io.modifier.basic.emo.scheme.SchemeIntArg
import io.modifier.basic.emo.scheme.SchemeLongArg
import io.modifier.basic.emo.scheme.SchemeStringArg
import io.modifier.basic.emo.scheme.SchemeTransition
import io.modifier.basic.emo.scheme.model
import io.modifier.basic.emo.scheme.readAction
import io.modifier.basic.emo.scheme.readTransition
import io.wongxd.demo.EmoScheme
import io.wongxd.demo.page.emo.EmoMainActivity
import io.wongxd.demo.page.emo.config.SchemeConst
import io.wongxd.demo.page.emo.config.runQuietly
import io.wongxd.demo.page.emo.config.schemeBuilder
import io.wongxd.demo.page.emo.config.webSchemeBuilder
import io.wongxd.demo.page.emo.ui.CommonItem
import kotlinx.serialization.Serializable

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME,
    alternativeHosts = [EmoMainActivity::class],
    transition = SchemeTransition.PUSH_THEN_STILL
)
@Composable
fun SchemePage(navBackStackEntry: NavBackStackEntry) {
    Log.i("SchemePage", "action = ${navBackStackEntry.readAction()}, transition = ${navBackStackEntry.readTransition()}")
    OnlyBackListPage(
        title = "Scheme"
    ) {
        item {
            CommonItem("For Activity") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_ACTIVITY).runQuietly()
            }
        }

        item {
            CommonItem("For Composable") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_MODAL).forceNewHost().runQuietly()
            }
        }

        item {
            CommonItem("With Argument") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_ARG)
                    .arg("str", "hehe")
                    .arg("int", 100)
                    .arg("xxx", "xx")
                    .runQuietly()
            }
        }

        item {
            CommonItem("With Model Argument") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_MODEL)
                    .model(DataArg(str = "haha"))
                    .runQuietly()
            }
        }

        item {
            CommonItem("Host Special Argument") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_HOST_ARG)
                    .arg("aa", "hehe")
                    .runQuietly()
            }
        }

        item {
            CommonItem("Special Arg, Type = 1") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_SPECIAL_ARG)
                    .arg("type", 1)
                    .runQuietly()
            }
        }

        item {
            CommonItem("Special Arg, Type = 2") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_SPECIAL_ARG)
                    .arg("type", 2)
                    .runQuietly()
            }
        }

        item {
            CommonItem("Alpha Transition") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_ALPHA).runQuietly()
            }
        }

        item {
            CommonItem("Slide From Bottom") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_SLIDE_BOTTOM).runQuietly()
            }
        }

        item {
            CommonItem("Batch") {
                val schemes = listOf(
                    schemeBuilder(SchemeConst.SCHEME_ACTION_PHOTO).toString(),
                    webSchemeBuilder("file:///android_asset/demo.html", "JsBridge").toString(),
                    schemeBuilder(SchemeConst.SCHEME_ACTION_MODAL).toString(),
                    schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_ACTIVITY).toString(),
                    schemeBuilder(SchemeConst.SCHEME_ACTION_PERMISSION).toString()
                )
                EmoScheme.batchHandleQuietly(schemes)
            }
        }
    }
}

@Serializable
data class DataArg(
    val i: Int = 3,
    val l: Long = 4,
    val b: Boolean = true,
    val str: String = "xixi",
)

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME_MODEL,
    alternativeHosts = [EmoMainActivity::class]
)
@Composable
fun SchemeModelPage(arg: DataArg) {
    OnlyBackListPage(
        title = "SchemeArg"
    ) {
        item {
            CommonItem(arg.toString()) {
            }
        }
    }
}

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME_ARG,
    alternativeHosts = [EmoMainActivity::class]
)
@SchemeStringArg(name = "str", default = "xx")
@SchemeIntArg(name = "int", default = 3)
@SchemeLongArg(name = "long", default = 3)
@SchemeFloatArg(name = "float", default = 3.14f)
@SchemeBoolArg(name = "bool", default = false)
@Composable
fun SchemeArgPage(navBackStackEntry: NavBackStackEntry) {
    val stringArg = navBackStackEntry.arguments?.getString("str")
    val intArg = navBackStackEntry.arguments?.getInt("int")
    val longArg = navBackStackEntry.arguments?.getLong("long")
    val boolArg = navBackStackEntry.arguments?.getBoolean("bool")
    val floatArg = navBackStackEntry.arguments?.getFloat("float")
    OnlyBackListPage(
        title = "SchemeArg"
    ) {
        item {
            CommonItem("str = $stringArg, int = $intArg, long = $longArg, bool = $boolArg, float = $floatArg") {
            }
        }
    }
}

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME_HOST_ARG,
    alternativeHosts = [EmoMainActivity::class]
)
@SchemeStringArg(name = "aa", default = "hehe")
@Composable
fun SchemeHostArgPage(navBackStackEntry: NavBackStackEntry) {
    val aaArg = navBackStackEntry.arguments?.getString("aa")
    OnlyBackListPage(
        title = "SchemeHostArg($aaArg)"
    ) {
        item {
            CommonItem("Matched, Use current activity") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_HOST_ARG)
                    .arg("aa", aaArg ?: "")
                    .runQuietly()
            }
        }

        item {
            CommonItem("Not matched, Use new activity") {
                schemeBuilder(SchemeConst.SCHEME_ACTION_SCHEME_HOST_ARG)
                    .arg("aa", "${System.currentTimeMillis()}")
                    .runQuietly()
            }
        }
    }
}

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME_ALPHA,
    alternativeHosts = [EmoMainActivity::class],
    transition = SchemeTransition.SCALE
)
@Composable
fun SchemeAlphaTransitionPage() {
    OnlyBackListPage(
        title = "SchemeAlphaTransition"
    ) {
    }
}

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME_SLIDE_BOTTOM,
    alternativeHosts = [EmoMainActivity::class],
    transition = SchemeTransition.PRESENT
)
@Composable
fun SchemeSlideFromBottomPage() {
    OnlyBackListPage(
        title = "SchemeSlideFromBottom"
    ) {
    }
}

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME_SPECIAL_ARG,
    alternativeHosts = [EmoMainActivity::class]
)
@SchemeIntArg(name = "type", special = true, default = 1)
@Composable
fun SchemeSpecialArgType1(navBackStackEntry: NavBackStackEntry) {
    OnlyBackListPage(
        title = "SchemeSpecialArg(Type = 1)"
    ) {
    }
}

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_SCHEME_SPECIAL_ARG,
    alternativeHosts = [EmoMainActivity::class]
)
@SchemeIntArg(name = "type", special = true, default = 2)
@Composable
fun SchemeSpecialArgType2(navBackStackEntry: NavBackStackEntry) {
    OnlyBackListPage(
        title = "SchemeSpecialArg(Type = 2)"
    ) {
    }
}
